﻿using GodSystemCommon;
using GodSystemModel;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GodSystemDAL
{
    public class PowersDAL
    {

        public MySqlDataReader GetUserPowerByUserId(UserrolepowerModel userrolepowerModel)
        {
            string sql = "select Id,PowerId,UserId from Userrolepower where UserId = @UserId";
            MySqlParameter[] pms = {
                new MySqlParameter("@UserId", userrolepowerModel.UserId)
            };
            return SqlHelper.ExecuteReader(sql, pms);
        }
        public object GetPowerGroup(PowerGroupModel powerGroupModel)
        {
            string sql = "select count(*) as total from PowerGroup where GroupName = @GroupName";
            MySqlParameter[] pms = {
               new MySqlParameter("@GroupName", powerGroupModel.GroupName)
            };
            return SqlHelper.ExecuteScalar(sql, pms);
        }

        public int UpdatePower(PowersModel powers)
        {
            string sql = "update Powers set Remark = @Remark,PowerGroupId = @PowerGroupId where Id = @Id";
            MySqlParameter[] pms =
            {
                new MySqlParameter("@Remark", powers.Remark),
                new MySqlParameter("@PowerGroupId", powers.PowerGroupId),
                new MySqlParameter("@Id", powers.Id),
            };
            return SqlHelper.ExecuteNonQuery(sql, pms);
        }

        public object GetPowerByIdAndName(PowersModel powers)
        {
            string sql = "select count(*) as total from Powers  where  Remark = @Remark and Id != @Id";
            MySqlParameter[] pms =
            {
                new MySqlParameter("@Remark", powers.Remark),
                new MySqlParameter("@Id", powers.Id),
            };
            return SqlHelper.ExecuteScalar(sql, pms);
        }

        public int AddPowerGroup(PowerGroupModel powerGroupModel)
        {
            string sql = "insert into PowerGroup(GroupName) values(@GroupName)";
            MySqlParameter[] pms = { 
                new MySqlParameter("@GroupName", powerGroupModel.GroupName)
            };
            return SqlHelper.ExecuteNonQuery(sql, pms);
        }


        public MySqlDataReader GetPowerGroup()
        {
            string sql = "select Id,GroupName from PowerGroup";
            return SqlHelper.ExecuteReader(sql);
        }

        /// <summary>
        /// 获取角色拥有的权限
        /// </summary>
        /// <param name="rolepower"></param>
        /// <returns></returns>
        public MySqlDataReader GetRolepower(RolepowerModel rolepower)
        {
            string sql = "select Id,RoleId,PowerId from Rolepower where RoleId = @Id";
            MySqlParameter[] pms = {
                new MySqlParameter("@Id", rolepower.Id)
            };
            return SqlHelper.ExecuteReader(sql, pms);
        }

        public object GetPower(PowersModel powers)
        {
            string sql = "select count(*) as total from Powers where Node = @Node or Remark=@Remark";
            MySqlParameter[] pms = {
               new MySqlParameter("@Remark", powers.Remark),
               new MySqlParameter("@Node", powers.Node),
            };
            return SqlHelper.ExecuteScalar(sql, pms);
        }


        public int AddPower(PowersModel powers)
        {
            string sql = "insert into Powers(Node,Remark,PowerGroupId) values(@Node,@Remark,@PowerGroupId)";
            MySqlParameter[] pms =
            {
                new MySqlParameter("@Node", powers.Node),
                new MySqlParameter("@Remark", powers.Remark),
                new MySqlParameter("@PowerGroupId", powers.PowerGroupId),
            };
            return SqlHelper.ExecuteNonQuery(sql, pms);
        }


        public MySqlDataReader GetPowerByRoleIds(string roleIds)
        {
            string sql = "select Id,RoleId,PowerId from Rolepower where  find_in_set(RoleId, @roleIds)";
            MySqlParameter[] pms =
            {
                new MySqlParameter("@roleIds", roleIds)
            };
            return SqlHelper.ExecuteReader(sql, pms);
        }


        public MySqlDataReader GetPowerByPowerid(string powerIds)
        {
            string sql = "select Id,Node,Remark,PowerGroupId from Powers where  find_in_set(Id, @powerIds)";
            MySqlParameter[] pms =
            {
                new MySqlParameter("@powerIds", powerIds)
            };
            return SqlHelper.ExecuteReader(sql, pms);
        }

        public MySqlDataReader GetPowergroupById(string ids)
        {
            string sql = "select Id,GroupName from Powergroup where  find_in_set(Id, @ids)";
            MySqlParameter[] pms =
            {
                new MySqlParameter("@ids", ids)
            };
            return SqlHelper.ExecuteReader(sql, pms);
        }



        public MySqlDataReader GetAllPowers()
        {
            string sql = "select Id,Node,Remark,PowerGroupId from Powers order by PowerGroupId";
            return SqlHelper.ExecuteReader(sql);
        }


        public int UpdateRolePower(List<int> addPowerId, int roleId)
        {
            try
            {
                SqlHelper.StartTran();
                string delSql = "delete from Rolepower where RoleId = @RoleId";
                MySqlParameter[] pms =
                {
                    new MySqlParameter("@RoleId", roleId),
                };
                SqlHelper.ExecuteNonQueryForTran(delSql, pms);
                if (addPowerId.Count > 0)
                {
                    string addSql = "insert into Rolepower(RoleId,PowerId) values ";
                    foreach (int id in addPowerId)
                    {
                        addSql += "(" + roleId + "," + id + "),";
                    }
                    int length = addSql.Length - 1;
                    addSql = addSql.Substring(0, length);
                    SqlHelper.ExecuteNonQueryForTran(addSql);
                }
                SqlHelper.TransactionCommit();
                return 1;
            }
            catch (Exception)
            {
                SqlHelper.TransactionRollback();
                return 0;
            }
            
        }
    }
}
